import { useSize } from "ahooks";
import { useEffect, useLayoutEffect, useRef, useState } from "react";
import { useForm } from "../../../framework/common/context/metadata-context";
import eventBus from "../../../framework/common/eventBus";
import { fieldFilter, fieldFilterNormal } from "../../../framework/common/utils/filter";
import Cell from "./Cell";
import jQuery from "jquery";
// export default function (props) {
//   const [isReady,setIsReady]=useState(false)
//   useEffect(()=>{

//     // setTimeout(() => {
//       setIsReady(true)
//     // }, 0);
    
//   },[])

//   if(isReady===false){
//     return null
//   }
//   return <A  {...props}></A>
// }

export default function  ({
  disableHeightCalculcate,
  data,
  isSummary = false,
  row,
  isFromFixedTable = false,
  rowIndex,
  groupCode,
  group,
  fields,
  // filterFields,
  // normalFilterFields,
  isFiexedTable = false,
  fixedColum = 999,
  dataPrefix = "form.form_data.children.",
  isVirtualTable
}) {
  const form = useForm().root;
  let mainField = `${dataPrefix}${groupCode}[${rowIndex}].`;
  const rowRef = useRef();
  const preSizeRef = useRef();
  let isPrint=(form.mode==="Print")
  // const size = useSize(rowRef);
  //如果是固定列模式,收集行高度
  useEffect(() => {
   // console.log("row useLayoutEffect", fixedColum);
    if (fixedColum === 999 &&rowRef.current) {
      group.cachedHeight[rowIndex] = { height:rowRef.current?.getBoundingClientRect()?.height || rowRef.current.offsetHeight};
      // console.log(group.code,rowIndex,jQuery(rowRef.current).height(),rowRef.current,size?.height)
      // setTimeout(() => {
      //   eventBus.publish(`group.fiexedTableUpdate.${group.code}`);
      // }, 0);


     // console.log("row",rowIndex,group.code,size)
    }
  });
  // if (fixedColum === 999 && size && preSizeRef.current?.height !== size.height) {
  //   group.cachedHeight[rowIndex] = { height: jQuery(rowRef.current).height() };
  //   preSizeRef.current = size;
  // }

  //如果是固定列,设置行高度
  let rowStyle;
  if (fixedColum !== 998&& disableHeightCalculcate!==true) {
    //打印页面不需要设置行高
    if(!isPrint)
      rowStyle = group.cachedHeight[rowIndex];
  }

  let filterFields = fields.filter(fieldFilter);

  let normalFilterFields = fields.filter(fieldFilterNormal);
  let shouldHiddenRow = true;


  let allRowHiddenCount = 0;

  let fixedColumCount = fixedColum;
  normalFilterFields.forEach((field, index) => {
    if (field.isAllRowhidden === true && index <= fixedColum) {
      allRowHiddenCount++;
    }
  });
  fixedColumCount = fixedColumCount - allRowHiddenCount;

  //如果没有field需要显示 就隐藏整个行
  for (let index = 0; index < filterFields.length; index++) {
    const element = filterFields[index];
    if (element.isHiddenRows) {
      if (element.isHiddenRows?.[rowIndex] !== true) {
        shouldHiddenRow = false;
        break;
      }
    } else {
      shouldHiddenRow = false;
      break;
    }
  }

  if (shouldHiddenRow) {
    return null;
  }

  //编辑模式下子表删除列
  let operationCell = undefined;
  // if (form.isDesign && group.isDelete && isFromFixedTable !== true) {
  //   operationCell = (
  //     <td className="text-center border-l text-primary">
  //       <i title="删除" className="cursor-pointer iconfont icon-shanchu"></i>
  //     </td>
  //   );
  // }

  let hasSid= !!filterFields.find(field=>field.code === "table_sid")

  return (
    <tr ref={rowRef} style={rowStyle}>
      {filterFields.map((field, cellIndex) => {
        if (cellIndex >= fixedColumCount) {
          return null;
        }
        if (field.isHidden === true) {
          return null;
        }

        let fieldCodePath = mainField + field.code;

        return <Cell hasSid={hasSid} isVirtualTable={isVirtualTable} isSummary={isSummary} row={row} key={`${row.__cid__}.${field.code}`} cellIndex={cellIndex} rowIndex={rowIndex} codePath={fieldCodePath} groupCode={groupCode} cell={field}></Cell>;
      })}
      {operationCell}
    </tr>
  );
}
